﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Cache;

namespace proxy4
{
    public partial class infoWnd : Form
    {
        public MainWindow parentWnd;

        public infoWnd()
        {
            InitializeComponent();
            
            // 显示内容
            this.CenterToScreen();
            try
            {
                // Webbrowser settings


                // Show content
                if (!Directory.Exists("data"))
                {
                    Directory.CreateDirectory("data");
                }
                // display local version
                string location = String.Format("file:///{0}/data/info.html", Directory.GetCurrentDirectory());
                Uri infoHtmlUri = new Uri(location);
                infoFrame.Navigate( infoHtmlUri );

            }
            catch (Exception e)
            {
                // debug
                //MessageBox.Show(e.Message);
            }
        }

        public int updateContent()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    
                    // update the HTML
                    client.DownloadFileAsync(new Uri("http://cernterm.sinaapp.com/Index/info"), Directory.GetCurrentDirectory() + "/data/info.html");
                    return 0;
                }
            }
            catch
            {
                return -1;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 异步显示
        /// </summary>
        public void AsynShow()
        {

            infoFrame.Navigate(new Uri("http://cernterm.sinaapp.com/Index/info"));

            // 触发百度统计
            TriggerBaiduTongji();

            infoFrame.DocumentCompleted += infoFrame_DocumentCompleted;

        }

        /// <summary>
        /// 触发百度统计 代码
        /// </summary>
        void TriggerBaiduTongji()
        {
            // No Cache Policy
            HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            HttpWebRequest.DefaultCachePolicy = policy;

            var request = (HttpWebRequest)HttpWebRequest.Create("http://www.solesschong.com/software/proxy.htm");

            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            request.CachePolicy = noCachePolicy;
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            // Trivial Settings
            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 8982 )";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Timeout = 120000;
            try
            {
                HttpWebResponse oWebResp = (HttpWebResponse)request.GetResponse();

                //                StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(), System.Text.Encoding.Default);


                string sResp = oStream.ReadToEnd();

                WebBrowser webBrowser1 = new WebBrowser();
                webBrowser1.Navigate("about:blank");
                webBrowser1.Document.Write(sResp);
                webBrowser1.Refresh();

            }
            catch { }
        }

        void infoFrame_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //this.Show();
            //this.TopLevel = true;
            this.TopMost = true;
            parentWnd.SlideInfoCallback();
        }
    }
}
